package logic

import (
	"context"
	"strings"

	"mall/service/recommended/api/internal/svc"
	"mall/service/recommended/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserNeighborsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserNeighborsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserNeighborsLogic {
	return &GetUserNeighborsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserNeighborsLogic) GetUserNeighbors(req *types.UserNeighborsReq) (
	resp *types.NeighborsResp, err error,
) {
	resp = &types.NeighborsResp{}
	if len(strings.TrimSpace(req.UserId)) == 0 {
		return resp, nil
	}

	res, err := l.svcCtx.GorseClient.GetUserNeighbors(l.ctx, req.UserId, int(req.Page), int(req.PageSize))
	if err != nil {
		return resp, err
	}
	for _, v := range res {
		resp.List = append(resp.List, map[string]any{
			"id":    v.Id,
			"score": v.Score,
		})
	}
	return resp, nil
}
